/*
 * Copyright 2020 Liu Jie <jieliu2000@live.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.beyondsoft.sabg.edu.model.db.core;

import com.beyondsoft.sabg.edu.model.exception.GenericBusinessException;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.persistence.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 *
 * @author Liu Jie <jieliu2000@live.com>
 */
@Entity
@Table(name = "nothing-image")
public class Image extends BinaryFile {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@JsonProperty
	private Long id;

    @JsonIgnore
    Integer height;

    @JsonIgnore
    Integer width;

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BufferedImage getBufferedImage() throws Exception {
		InputStream is = new ByteArrayInputStream(this.getBinaryContents());
		BufferedImage bi = ImageIO.read(is);

		return bi;
	}

	@Override
	public void converFromMultipartFile(MultipartFile file) throws IOException {
        converFromMultipartFile(file, null, null);
    }

    public void converFromMultipartFile(MultipartFile file, Integer scaleWidth,
            Integer scaleHeight) throws IOException {
        InputStream is = file.getInputStream();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int height = 0;
        int width = 0;
        if (scaleWidth != null && scaleHeight != null) {
            Thumbnails.of(is).forceSize(scaleWidth, scaleHeight).outputFormat("png")
                    .toOutputStream(bos);
            height = scaleHeight;
            width = scaleWidth;
        } else {

            BufferedImage bi = ImageIO.read(is);
            if(bi == null){
                throw new GenericBusinessException("暂不支持该图片，请更换图片重试");
            }

            height = bi.getHeight();
            width = bi.getWidth();

            ImageIO.write(bi, "png", bos);
        }
        byte[] bytes = bos.toByteArray();

        this.setStoreLocation("db");
        this.setName(UUID.randomUUID().toString() + ".png");
        this.setContentType("image/png");
        this.setBinaryContents(bytes);
        this.setHeight(height);
        this.setWidth(width);
    }
}
